package apis

import (
	"time"
	"xiaoercaidan/baseApi/dao"
	"xiaoercaidan/publicResources/controller"
	"xiaoercaidan/publicResources/models"
	"encoding/json"
	"xiaoercaidan/publicResources/utils"
	"github.com/goinggo/mapstructure"
)

type ShopApi struct {
	controller.BaseController
}

// @Title AddShop
// @Description 新增店铺
// @Param   shop     patch string   true string       "门店json"
// @Success 200 {object} models.Shop
// @Failure 400 Invalid params supplied
// @Failure 404 page not found
// @router /add [get]
func (c *ShopApi) AddShop() {

	var BaseResult = new(models.BaseResult)

	var shopStr = c.GetString("shop")

	if ( len(shopStr) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "参数为空"
	} else {

		var mapStr map[string]interface{}
		var imageList []interface{}
		json.Unmarshal([]byte(shopStr), &mapStr)
		imageList = mapStr["ImageList"].([]interface{})
		for k := range mapStr {
			if k == "Id" {
				mapStr[k] = nil
			}
		}

		var shop = new(models.Shop)
		mapstructure.Decode(mapStr, shop);
		shop.CreateTime = time.Now().UTC().UnixNano()

		var ImageList   []models.ShopImage
		for _, value := range imageList {
			var image = new(models.ShopImage)
			mapstructure.Decode(value, image);
			ImageList = append(ImageList, *image)
		}
		shop.ImageList = ImageList

		if err := dao.InserShop(shop); (utils.CheckResult(BaseResult, err)) {
			BaseResult.Data = shop
		}

		for _, value := range ImageList {
			value.ShopId = shop.Id
			dao.InserShopImage(value)
		}

	}
	c.Data["json"] = BaseResult

	c.ServeJSON()

}



// @Title QueryShop
// @Description 查询店铺
// @Param   shopId     patch string   true int64       "门店Id"
// @Param   ShopName     patch string   true int64       "门店名称"
// @Param   Address     patch string   true int64       "门店地址"
// @Param   UserId     patch string   true int64       "用户ID"
// @Success 200 {object} models.Shop
// @Failure 400 Invalid params supplied
// @Failure 404 api not found
// @router /query [get]
func (this *ShopApi) QueryShop() {
	//page, _ := this.GetInt64("page")
	//pageSize, _ := this.GetInt64("rows")


	var shopId = this.GetString("shopId")
	var ShopName = this.GetString("shopName")
	var Address = this.GetString("address")
	var UserId = this.GetString("userId")

	var BaseResult = new(models.BaseResult)

	if ( len(UserId) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "用户ID不能为空"
	} else {

		var params = make(map[string]string)

		params["user_id"] = UserId

		if ( len(shopId) > 0) {
			params["id"] = shopId
		}

		if ( len(ShopName) > 0) {
			params["shop_name"] = ShopName
		}

		if ( len(Address) > 0) {
			params["address"] = Address
		}

		shop := dao.QeryShopBlur(params)

		if shop == nil {
			shop = new([]models.Shop)
		}

		BaseResult.Data = shop

	}

	this.Data["json"] = &BaseResult
	this.ServeJSON()
}

// @Title QueryShopById
// @Description 查询店铺
// @Param   shopId     patch string   true int64       "门店Id"
// @Success 200 {object} models.Shop
// @Failure 400 Invalid params supplied
// @Failure 404 api not found
// @router /queryShopById [get]
func (this *ShopApi) QueryShopById() {
	var shopId = this.GetString("shopId")
	var BaseResult = new(models.BaseResult)
	if ( len(shopId) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "ID不能为空"
	} else {
		shop := dao.QeryShopDetail(shopId)
		image := dao.QeryImageByShopId(shop.Id)
		if (image != nil) {
			shop.ImageList = *image
		}
		BaseResult.Data = shop
	}
	this.Data["json"] = &BaseResult
	this.ServeJSON()
}



// @Title ShopDel
// @Description 店铺刪除
// @Param   ids     patch string   true int64       "门店ID列表，以'，'分割"
// @Failure 400 Invalid params supplied
// @Failure 404 api not found
// @router /del [get]
func (this *ShopApi) DelShop() {

	var BaseResult = new(models.BaseResult)

	var idstr = ""

	this.Ctx.Input.Bind(&idstr, "ids")

	err := dao.DeleteShop(idstr)

	if (err != nil) {
		BaseResult.Code = 100
		BaseResult.Msg = err.Error()
	} else {
		BaseResult.Msg = "删除成功"
	}

	this.Data["json"] = &BaseResult
	this.ServeJSON()
}


// @Title ShopUpdate
// @Description 店铺修改
// @Param   shop     patch string   true string       "商品类json数据"
// @Failure 400 Invalid params supplied
// @Failure 404 api not found
// @router /update [get]
func (this *ShopApi) ShopUpdate() {

	var BaseResult = new(models.BaseResult)
	var shop = new(models.Shop)

	var shopJson = this.GetString("shop")

	if ( len(shopJson) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "参数不能为空"
	} else {

		json.Unmarshal([]byte(shopJson), &shop)

		err := dao.UpdateShop(shop)

		if (utils.CheckResult(BaseResult, err)) {
			BaseResult.Data = shop
		}
	}

	this.Data["json"] = &BaseResult
	this.ServeJSON()

}


// @Title GetShopByQrcode
// @Description 预约排号
// @Param   shopCode     patch string   true string       "店铺二维码"
// @Failure 400 Invalid params supplied
// @Failure 404 api not found
// @router /getShopByQrcode [get]
func (this *ShopApi) GetShopByQrcode() {

	var BaseResult = new(models.BaseResult)

	var params = make(map[string]string)

	var shopCode = this.GetString("shopCode")

	params["code"] = shopCode

	var shops = dao.QeryShopBlur(params)

	if (shops != nil ) {
		BaseResult.Data = shops
	} else {
		BaseResult.Code = 100
		BaseResult.Msg = "二维码错误，没有匹配到商家"
	}

	this.Data["json"] = &BaseResult

	this.ServeJSON()

}



